package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hospital.workstation.constant.DictConstants;
import com.hospital.workstation.domain.GradingFirst;
import com.hospital.workstation.domain.PatientBaseInfo;
import com.hospital.workstation.mapper.GradingFirstMapper;
import com.hospital.workstation.mapper.PatientBaseInfoMapper;
import com.hospital.workstation.utils.CareLevelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.GradingTwoBo;
import com.hospital.workstation.domain.vo.GradingTwoVo;
import com.hospital.workstation.domain.GradingTwo;
import com.hospital.workstation.mapper.GradingTwoMapper;
import com.hospital.workstation.service.IGradingTwoService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 医学二级监护分级信息Service业务层处理
 *
 * @author whb
 * @date 2023-05-22
 */
@RequiredArgsConstructor
@Service
public class GradingTwoServiceImpl implements IGradingTwoService {

    private final GradingTwoMapper baseMapper;
    private final PatientBaseInfoMapper patientBaseInfoMapper;
    private final GradingFirstMapper gradingFirstMapper;

    /**
     * 查询医学二级监护分级信息
     */
    @Override
    public GradingTwoVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询医学二级监护分级信息列表
     */
    @Override
    public TableDataInfo<GradingTwoVo> queryPageList(GradingTwoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GradingTwo> lqw = buildQueryWrapper(bo);
        Page<GradingTwoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询医学二级监护分级信息列表
     */
    @Override
    public List<GradingTwoVo> queryList(GradingTwoBo bo) {
        LambdaQueryWrapper<GradingTwo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GradingTwo> buildQueryWrapper(GradingTwoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GradingTwo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMedicalRecordNo() != null, GradingTwo::getMedicalRecordNo, bo.getMedicalRecordNo());
        lqw.eq(StringUtils.isNotBlank(bo.getItemOne()), GradingTwo::getItemOne, bo.getItemOne());
        lqw.eq(StringUtils.isNotBlank(bo.getItemTwo()), GradingTwo::getItemTwo, bo.getItemTwo());
        lqw.eq(StringUtils.isNotBlank(bo.getItemThree()), GradingTwo::getItemThree, bo.getItemThree());
        lqw.eq(StringUtils.isNotBlank(bo.getItemFour()), GradingTwo::getItemFour, bo.getItemFour());
        lqw.eq(StringUtils.isNotBlank(bo.getItemFive()), GradingTwo::getItemFive, bo.getItemFive());
        lqw.eq(StringUtils.isNotBlank(bo.getItemSix()), GradingTwo::getItemSix, bo.getItemSix());
        lqw.eq(StringUtils.isNotBlank(bo.getItemSeven()), GradingTwo::getItemSeven, bo.getItemSeven());
        lqw.eq(StringUtils.isNotBlank(bo.getItemEight()), GradingTwo::getItemEight, bo.getItemEight());
        lqw.eq(StringUtils.isNotBlank(bo.getItemNine()), GradingTwo::getItemNine, bo.getItemNine());
        lqw.eq(StringUtils.isNotBlank(bo.getItemTen()), GradingTwo::getItemTen, bo.getItemTen());
        lqw.eq(StringUtils.isNotBlank(bo.getItemEleven()), GradingTwo::getItemEleven, bo.getItemEleven());
        lqw.eq(StringUtils.isNotBlank(bo.getItemTwelve()), GradingTwo::getItemTwelve, bo.getItemTwelve());
        lqw.eq(StringUtils.isNotBlank(bo.getItemThirteen()), GradingTwo::getItemThirteen, bo.getItemThirteen());
        lqw.eq(StringUtils.isNotBlank(bo.getModerateRenalFunction()), GradingTwo::getModerateRenalFunction, bo.getModerateRenalFunction());
        lqw.eq(StringUtils.isNotBlank(bo.getAltValue()), GradingTwo::getAltValue, bo.getAltValue());
        lqw.eq(StringUtils.isNotBlank(bo.getAstValue()), GradingTwo::getAstValue, bo.getAstValue());
        lqw.eq(StringUtils.isNotBlank(bo.getAlpValue()), GradingTwo::getAlpValue, bo.getAlpValue());
        lqw.eq(StringUtils.isNotBlank(bo.getTbil()), GradingTwo::getTbil, bo.getTbil());
        lqw.eq(StringUtils.isNotBlank(bo.getHighWarningPills()), GradingTwo::getHighWarningPills, bo.getHighWarningPills());
        lqw.eq(StringUtils.isNotBlank(bo.getCareLevelValue()), GradingTwo::getCareLevelValue, bo.getCareLevelValue());
        return lqw;
    }

    /**
     * 新增医学二级监护分级信息
     */
    @Override
    public Boolean insertByBo(GradingTwoBo bo) {
        GradingTwo add = BeanUtil.toBean(bo, GradingTwo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改医学二级监护分级信息
     */
    @Override
    public Boolean updateByBo(GradingTwoBo bo) {
        GradingTwo update = BeanUtil.toBean(bo, GradingTwo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GradingTwo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除医学二级监护分级信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取医学二级监护分级信息详细信息
     *
     * @param medicalRecordNo 病历号
     * @return
     */
    @Override
    public GradingTwoVo queryByMedicalRecordNo(Integer medicalRecordNo) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<GradingTwo>().eq(GradingTwo::getMedicalRecordNo, medicalRecordNo));
    }

    /**
     * 更新监护二级信息和患者监护等级
     *
     * @param bo
     * @return
     */
    @Transactional
    @Override
    public Boolean editGradingTwoByBo(GradingTwoBo bo) {
        try {
            // 更新二级监护分级信息
            GradingTwo gradingTwo = BeanUtil.toBean(bo, GradingTwo.class);
            baseMapper.updateById(gradingTwo);
            // 查询患者的基本信息
            PatientBaseInfo baseInfo = patientBaseInfoMapper.selectOne(new LambdaQueryWrapper<PatientBaseInfo>().eq(PatientBaseInfo::getMedicalRecordNo, gradingTwo.getMedicalRecordNo()));
            if (ObjectUtil.isNotNull(gradingTwo) && ObjectUtil.isNotNull(baseInfo)) {
                // 判断是否属于一级监护
                GradingFirst gradingFirst = gradingFirstMapper.selectOne(new LambdaQueryWrapper<GradingFirst>().eq(GradingFirst::getMedicalRecordNo, gradingTwo.getMedicalRecordNo()));
                boolean isFirstGrading = CareLevelUtil.isFirstGrading(gradingFirst);
                if (isFirstGrading) { // 属于一级监护
                    if (!DictConstants.GUARDIAN_LEVEL_THREE.equals(baseInfo.getGuardianLevel())) { // 当前监护不是一级监护，则更新为一级监护
                        baseInfo.setGuardianLevel(DictConstants.GUARDIAN_LEVEL_THREE);
                    }
                } else {
                    // 判断是否属于二级监护
                    boolean isTwoGrading = CareLevelUtil.isTwoGrading(gradingTwo);
                    if (isTwoGrading) { // 属于二级监护
                        baseInfo.setGuardianLevel(DictConstants.GUARDIAN_LEVEL_TWO);
                    } else {
                        baseInfo.setGuardianLevel(DictConstants.GUARDIAN_LEVEL_ONE);
                    }
                    // 更新患者药学监护分级
                    patientBaseInfoMapper.updateById(baseInfo);
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
